function LoanUserGd() {
    $("#year").val($("#hidyear").val());
    this.form = this.InitForm();
    this.table = this.InitTable();
    this.InitOrg();
}

LoanUserGd.prototype.InitForm = function () {
    var form;
    layui.use('form', function () {
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功

        form.on('select(year)', function (data) {
            vLoanUserGd.searchtable();
        });
        form.render();
    });
    return form;
}

LoanUserGd.prototype.InitOrg = function () {
    var comboboxm = $("#orgid").ligerComboBox({
        valueField: 'orgid',
        textField: 'orgname',
        selectBoxWidth: 250,
        selectBoxHeight: 250,
        treeLeafOnly: false,
        valueFieldID: "hid",
        tree: {
            url: '../common/get_org_list?orgtype=0',
            checkbox: false,
            nodeWidth: 150,
            ajaxType: 'get',
            idFieldName: 'orgid',
            textFieldName: "orgname",
            idField: "orgid",
            parentIDField: "orgparentid"
        },
        onSelected: function (orgid, orgname) {// id为空值，name为选择的值
            $("#hidorgid").val(orgid);
            vLoanUserGd.searchtable();
        },
        width: 250
    });
    return comboboxm;
}

LoanUserGd.prototype.InitTable = function () {
    var table;
    var _this = this;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#list'
            , height: $(window).height() - 100
            , url: 'get_gd_list' // 数据接口
            , page: true // 开启分页
            , limit: 40
            , id: 'Loanuserlist'
            , limits: [20, 40, 80, 100]
            , method: "post"
            , cols: [[ // 表头
                //{type:'numbers'}
                {field: 'realname', title: '姓名', width: 130, align: 'center', fixed: 'left', templet: '#realnameTpl'}
                //, {field : 'sex',title : '性别',width : 80,align : 'center'}
                , {field: 'cardno', title: '身份证号', width: 180, align: 'center', event: 'glxs'}
                , {field: 'orgname', title: '实际工作单位', width: 200, align: 'center', event: 'glxs'}
                , {field: 'rsgxsorgname', title: '人事关系所在单位', width: 200, align: 'center', event: 'glxs'}
                , {field: 'loanorgname', title: '借调部门', width: 200, align: 'center', event: 'glxs'}
                , {field: 'status', title: '状态', width: 100, align: 'center', event: 'glxs'}
                , {field: 'begintime', title: '借调开始时间', width: 120, align: 'center', event: 'glxs'}
                , {field: 'endtime', title: '借调结束时间', width: 120, align: 'center', event: 'glxs'}
                //, {field : 'loanorgname',title : '借调部门',width : 200,align : 'center',event:'glxs'}
                , {field: 'byrealname', title: '填报人', width: 100, align: 'center', event: 'glxs'}
                , {field: 'createtime', title: '时间', width: 170, align: 'center', event: 'glxs'}
                , {field: 'loandes', title: '备注', width: 150, align: 'center', event: 'glxs'}
                , {title: '操作', width: 100, align: 'center', toolbar: '#operate'}
            ]]
            , request: {
                type: 'post'
            }
            , where: {
                year: $("#year").val(),
                orgid: $("#hidorgid").val(),
                realname: $("#realname").val()
            }
            , done: function (res, page, count) {

                var dataarr = res.data;
                $("[data-field='begintime']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        if (text != "" && text != null) {
                            var begin = (new Date(Date.parse(text.replace(/-/g, "/")))).format("yyyy-MM-dd");
                            $(this).text(begin);
                        }
                    }
                })
                $("[data-field='endtime']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        if (text != "" && text != null) {
                            var begin = (new Date(Date.parse(text.replace(/-/g, "/")))).format("yyyy-MM-dd");
                            $(this).text(begin);
                        }
                    }
                })




                $("[data-field='sex']").children().each(function (index) {
                    var text = $(this).text();
                    if (text == "0") {
                        $(this).text('男')
                    }
                    else if (text == "1") {
                        $(this).text('女')
                    }
                })
                $("[data-field='status']").children().each(function (index) {
                    var text = $(this).text();
                    switch (text) {
                        case "0":
                            $(this).text("填报");
                            break;
                        case "1":
                            $(this).text("提交");
                            break;
                        case "2":
                            $(this).text("归档");
                            break;
                        case "3":
                            $(this).text("驳回");
                            break;
                        case "4":
                            $(this).text("接收");
                            break;
                        case "5":
                            $(this).text("");
                            break;
                        case "6":
                            $(this).text("申请会原单位");
                            break;
                        case "7":
                            $(this).text("回原单位归档");
                            break;
                    }
                })


            }

        });


        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {

                top.mylayui.confirm('确定要删除吗？', {icon: 0}, function (index, dd) {
                    top.mylayui.close(index);
                    vUser.deleteentity(data.userid);
                });
            } else if (obj.event === 'edit') {
                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                //_this.OpTab(2,data);
                vUser.openadd(obj.data.userid);
            }
            else if (obj.event === 'btngd') {
                vLoanUserGd.openadd(obj.data.userid, 2, obj.data.loanid,obj.data.loanorgid);
            }
            else if (obj.event === 'btnbh') {
                vLoanUserGd.openadd(obj.data.userid, 3, obj.data.loanid,obj.data.loanorgid);
            }
        });


    });

    return table;
}

LoanUserGd.prototype.searchtable = function () {
    this.table.reload('Loanuserlist', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , where: {
            year: $("#year").val(),
            orgid: $("#hidorgid").val(),
            realname: $("#realname").val()
        }
    });
}

LoanUserGd.prototype.openadd = function (userid, status, loanid,loanorgid) {
    var title = "";
    if (status == 2) {
        title = '归档';
    }
    else if (status == 3) {
        title = '驳回';
    }
    console.log("url");
    top.mylayui.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        area: ['500px', '300px'],
        content: 'loanuser/loan_gd_edit?userid=' + userid + '&status= '+status + '&loanid=' + loanid+"&loanorgid="+loanorgid
    });

    var iframeele = top.$(".layui-layer-content").find('iframe');
    iframeele.css({"height": $(iframeele).height() - 10});
}

LoanUserGd.prototype.addresult = function (result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        vLoanUserGd.searchtable();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}

var vLoanUserGd;
$(function () {
    vLoanUserGd = new LoanUserGd();
})


function CloseDialog(result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        document.formedit.reset();
        //vOrg.searchtable();
        //vOrg.OpTab(1);
        document.location.reload();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}